local modid = 'webber_the_trainer'

local Widget = require 'widgets/widget'
---@type widget_scrollablelist
local ScrollableList = require "widgets/scrollablelist"
local TEMPLATES = require "widgets/redux/templates"
local UIAnim = require "widgets/uianim"
local Text = require "widgets/text"
local Image = require "widgets/image"

local webbert_panel_item = require 'core_webber_the_trainer/widgets/webbert_panel_item'

local btns = require 'core_webber_the_trainer/data/webbert_soul_buy_items'
local totoal_buy_items = #btns -- 商品目前总数

for k,v in ipairs(btns) do
    local hint = v.hint or ''
    v.hint = SUGAR_webber_the_trainer:limitMultiLineStringSingleLineMaxLen(hint,16)
end

---@class widget_webbert_panel : widget_widget
---@field panel widget_widget
---@field scrolllist widget_widget
---@field items widget_webbert_panel_item[]
---@field soul widget_uianim # 血契动画
---@field soul_num widget_text # 血契数量
---@field hint_bg widget_image
---@field hint_txt widget_text
local webbert_panel = Class(Widget,
---@param self widget_webbert_panel
function(self)
    Widget._ctor(self, 'webbert_panel') -- 调用基类构造函数

    self.panel = self:AddChild(TEMPLATES.RectangleWindow(400, 500, STRINGS.MOD_WEBBER_THE_TRAINER.UI.webbert_soul.title, {
        {text = 'X', cb = function() self.panel:Hide() end, offset = nil, style = 'small'},
    }))
    self.panel.title:SetSize(60)
    self.panel.title:SetPosition(0, -25)
    self.panel:SetVAnchor(ANCHOR_MIDDLE)
    self.panel:SetHAnchor(ANCHOR_MIDDLE)

    self.soul = self.panel:AddChild(UIAnim())
    self.soul:GetAnimState():SetBank("fx_webbert_soul")
    self.soul:GetAnimState():SetBuild("fx_webbert_soul")
    self.soul:GetAnimState():PlayAnimation("idle",true)
    self.soul:GetAnimState():SetDeltaTimeMultiplier(.5)
    self.soul:SetPosition(-130,190)
    self.soul:SetScale(.7,.9)

    self.soul_num = self.panel:AddChild(Text(NUMBERFONT, 30 , ''))
    self.soul_num:SetPosition(60, 190)
    self.soul_num:SetHAlign(ANCHOR_LEFT)
    self.soul_num:SetRegionSize(300,40)
    self.soul_num:SetString('0')

    self.items = {}
    for i,v in ipairs(btns) do
        self.items[i] = webbert_panel_item(v.label,v.fn_calccost(0),function ()
            if ThePlayer.replica.webbert_soul then
                local cost = v.fn_calccost(ThePlayer.replica.webbert_soul['buy_times_'..i]:value())
                local cur_soul = ThePlayer.replica.webbert_soul:GetSoul()
                -- 当血契数量足够时,才能发RPC
                if cur_soul >= cost then
                    -- 只需要将商品的index发送过去即可
                    SendModRPCToServer(GetModRPC(modid,'webbert_soul_buy_item'),i)
                end
            end
        end,function ()
            ThePlayer:DoTaskInTime(0,function ()
                if self.hint_txt then
                    self.hint_txt:SetString(v.hint)
                    self.hint_txt:Show()
                end
                if self.hint_bg then
                    self.hint_bg:Show()
                end
            end)
        end,function ()
            if self.hint_bg and self.hint_bg.shown then
                self.hint_bg:Hide()
            end
            if self.hint_txt and self.hint_txt.shown then
                self.hint_txt:Hide()
            end
        end)
    end

    self.scrolllist = self.panel:AddChild(ScrollableList(self.items, 400, 330, 35, 1, nil, nil, 100, nil, nil, nil, nil, nil,'GOLD'))


    self.hint_bg = self.panel:AddChild(Image('images/ui.xml', 'single_option_bg_large.tex'))
    self.hint_bg:SetPosition(400,0)
    self.hint_bg:SetScale(.6,2)
    self.hint_bg:Hide()
    self.hint_txt = self.panel:AddChild(Text(BODYTEXTFONT, 30, ' '))
    self.hint_txt:SetPosition(400,0)
    self.hint_txt:Hide()

    -- 监听血契值变化
    ThePlayer:ListenForEvent('webbert_soul_soul_num_change',function ()
        if ThePlayer.replica.webbert_soul then
            local soul = ThePlayer.replica.webbert_soul:GetSoul()
            self.soul_num:SetString(self:fnum(soul))
        end
    end)
    -- 监听购买次数变化
    for i = 1, totoal_buy_items do
        ThePlayer:ListenForEvent('webbert_soul_buy_times_change_'..i,function ()
            if ThePlayer.replica.webbert_soul then
                local cur_times = ThePlayer.replica.webbert_soul['buy_times_'..i]:value()
                self.items[i].soul:SetString(btns[i].fn_calccost(cur_times))
                self.items[i].have:SetString(cur_times)
            end
        end)
    end
end)

function webbert_panel:fnum(num)
    local formatted = ""
    local number_str = tostring(num)
    local length = #number_str

    for i = 1, length do
        local digit = number_str:sub(i, i)
        if (length - i) % 3 == 0 and i ~= length then
            digit = digit .. ","
        end
        formatted = formatted .. digit
    end

    return formatted
end

---comment
---@param items table<integer,string>
function webbert_panel:MakeBtn(items)
    if self.scrolllist ~= nil then
        self.scrolllist:Kill()
        self.scrolllist = nil
    end
    -- for k,_ in pairs(self.items or {}) do
    --     self.items[k]:Kill()
    --     self.items[k] = nil
    -- end
    -- self.items = {}
    -- for k,v in pairs(items) do
    --     self.items[k] = TEMPLATES.StandardButton(function()
    --         -- SendModRPCToServer(GetModRPC(modid, "gg_ccc_knife_ui_click"),k)
    --         self.panel:Hide()
    --     end , v, {200, 40})
    -- end

    self.scrolllist = self.panel:AddChild(ScrollableList(self.items, 400, 370, 35, 1, nil, nil, 200, nil, nil, nil, nil, nil,'GOLD'))
end


return webbert_panel